package com.diligreat.action; 

import java.util.List;

import org.springframework.util.StringUtils;

import com.diligreat.action.base.BaseAction;
import com.diligreat.domain.Content;
import com.diligreat.domain.Member;
import com.diligreat.service.IndexService;
import com.diligreat.service.MemberService;
import com.opensymphony.xwork2.ModelDriven;
import com.diligreat.domain.Selection;

/**  
 * @Author Bruce 
 * @Date 2012-9-30
 * @Comments
 */
public class MemberAction extends BaseAction implements ModelDriven<Member>{
	private static final long serialVersionUID = 1L;
	private String confPwd;//注册时的确认密码
	private Member mem = new Member();
	private MemberService memberService;
	
	
	private IndexService indexService;
	private List<Selection> listRelation;
	private List<Selection> listIndustry;
	private List<Selection> listResponsibility;
	private List<Selection> listLevel;
	private List<Selection> listRegion;
	
	private List<Content> articleslist;
	
	//---------------------- 注册  -----------------------
	public String register(){
		Long l=Long.parseLong((String) this.getHttpSession().getAttribute("lang"));
		
		this.listRelation=this.indexService.getSelection(l, 1);
		this.listIndustry=this.indexService.getSelection(l, 2);
		this.listResponsibility=this.indexService.getSelection(l, 3);
		this.listLevel=this.indexService.getSelection(l, 4);
		this.listRegion=this.indexService.getSelection(l, 5);
		
		return SUCCESS;
	}

	public String reg_submit(){
		
		if(mem == null){
			return "login";
		}
		if(!mem.getPwd().equals(confPwd)){
			return ERROR;
		}else{
			int id=memberService.register(mem);
			mem.setId(id);
			mem = memberService.login(mem);
			this.getHttpSession().setAttribute("mem", mem);
		}
		return SUCCESS;
	}
	
	public String logout()
	{
		this.getHttpSession().setAttribute("mem", null);
		return SUCCESS;
	}
	
	//---------------------- 登陆  ----------------------
	public String login(){
		return SUCCESS;
	}
	public String login_submit(){
		mem = memberService.login(mem);
		if(mem == null){
			this.addActionMessage("用户名/密码不匹配");
			return INPUT;
		}
		
		this.getHttpSession().setAttribute("mem", mem);
		
		return SUCCESS;
	}
	
	//=============================== 输入校验 ===============================
	/**
	 * 登录信息校验
	 * @return true-校验通过
	 */
	public void validateLogin_submit(){
		//用户名输入校验
		if(!StringUtils.hasText(mem.getEmail())){
			this.addFieldError("loginName", "电子邮件地址不能为空！");
		}else if(mem.getEmail().length() > 50){
			this.addFieldError("loginName","电子邮件地址输入过长！");
		}
		//密码输入校验
		if(!StringUtils.hasText(mem.getPwd())){
			this.addFieldError("password","密码不能为空！");
		}else if(mem.getPwd().length() > 50){
			this.addFieldError("password","密码输入过长！");
		}
	}
	
	public String update()
	{
		this.mem=(Member)this.getHttpSession().getAttribute("mem");
		
		
		return SUCCESS;
	}
	
	public String reg_update()
	{
		return SUCCESS;
	}
	
	
	public String mystuff()
	{
		Long l=Long.parseLong((String) this.getHttpSession().getAttribute("lang"));
		Member m=(Member)this.getHttpSession().getAttribute("mem");
		String t=m.getIndustryIds()+","+m.getRelation()+","+m.getResponsibilityIds();
		
		this.articleslist=this.indexService.getMyStuff(l, 1, t);
		
		
		return SUCCESS;
	}
	//=============================== getter and setter ===============================
	@Override
	public Member getModel() {
		return mem;
	}
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public void setConfPwd(String confPwd) {
		this.confPwd = confPwd;
	}
	
	public void setIndexService(IndexService indexService) {
		this.indexService = indexService;
	}

	public void setListIndustry(List<Selection> listIndustry) {
		this.listIndustry = listIndustry;
	}

	public List<Selection> getListIndustry() {
		return listIndustry;
	}

	public List<Selection> getListRelation() {
		return listRelation;
	}

	public void setListRelation(List<Selection> listRelation) {
		this.listRelation = listRelation;
	}

	public List<Selection> getListResponsibility() {
		return listResponsibility;
	}

	public void setListResponsibility(List<Selection> listResponsibility) {
		this.listResponsibility = listResponsibility;
	}

	public List<Selection> getListLevel() {
		return listLevel;
	}

	public void setListLevel(List<Selection> listLevel) {
		this.listLevel = listLevel;
	}

	public List<Selection> getListRegion() {
		return listRegion;
	}

	public void setListRegion(List<Selection> listRegion) {
		this.listRegion = listRegion;
	}

	public List<Content> getArticleslist() {
		return articleslist;
	}

	public void setArticleslist(List<Content> articleslist) {
		this.articleslist = articleslist;
	}
	
}
 